<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('help_class', LIB_PATH);

$main = new Help($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? $_POST['self'] : $_GET['self'];

$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

switch ($cf_action) {
    case "index":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$ary[''] = '全部';	

	$stype = isset($_POST['stype']) ? trim($_POST['stype']) : trim($_GET['stype']);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$self = isset($_POST['self']) ? trim($_POST['self']) : urldecode($_GET['self']);	
	$where = '1';
	

	
	if ($skey) {
	    $where .= " and help_title like '%" . $skey . "%'";
	}
	if (is_numeric($self)) {
	    $where .= " and help_sort = '" . $self . "'";
	}
	

	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "help_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&stype=' . $stype . '&self=' . $self);
	//echo $MyPage->get_sql();
	break;
    case "add":	
	if ($cf_ispost) {	    
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['help_create_time'] =  date("Y-m-d H:i:s");

	    //$db->debug(1,1);
	    $result = $main->insert_data($data);

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<br><a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失敗');
	    }
	} else {
		$part = $_GET['part'];
		//$help_sort = $ary + $cf_info_var['help']['help_sort'];
		$help_sort =  $cf_info_var['help']['help_sort'];
	    $smarty->mypage($cf_controller, $cf_action, 0);
		$row['questions'] = array();
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
	    //dump($_REQUEST);
	}

	break;
    case "edit":
	if ($cf_ispost) {
		$data = $db->getFormData($main->get_table_name(),$_POST);
		//$data['help_list'] =  implode("|",$_POST['help_list']);
		$data['help_edit_info'] =  $cf_admin_info['admin_name'] . ' ' . date("Y-m-d H:i:s");	
		//$db->debug(1,1);
	    $result = $main->update_data($data, "help_id='$id'");

	    if ($result) {
            $return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
            unset($_SESSION['pre_url']);
            my_success('修改成功', '', $return_page, 20);
            } else {
            my_error('修改失败');
	    }
	} else {
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');
		if($row['help_part'] == '0'){
			$help_sort = $cf_info_var['help'][3];			
		}elseif($row['help_part']== '1'){
			$help_sort = $cf_info_var['help'][2];
		}		
	    //dump($row);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
	}
	break;
    case "del":
	$id = intval($_GET['id']);
	$result = $main->del_data("help_id='$id'");

	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失敗!');
	}

	break;
    case "set":
	$type = $_GET['type'];
	$value = $_GET['value'];
	$where = "help_id = '" . $id . "'";
	$result = $main->set_data($value, $type, $where);
	if ($result) {

	    my_jump('更新完成', '', $return_page, 1);
	} else {
	    my_jump('更新失敗');
	}
	break;
    default:
	my_jump('非法操作!');
	break;
}

$smarty->assign(array(
    'id' => $id,
    'row' => htmlchar($row),
    'data_ary' => $data_ary,
    'page' => $page,
    'self' => $self,
    'skey' => $skey,
    'skey2' => $skey2,
    'skey3' => $skey3,
    'stype' => $stype,
	'help_sort' =>  add_array_item(array('' => '全部'), $cf_info_var['help']['help_sort']),
	

));


$smarty->mydisplay();
?>